﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using ScrappyDB.Linq;

namespace ScrappyDB.IntegrationTests.AwsQuery101Examples
{
    /************************************************************************************
    *   Query 101: Building Amazon SimpleDB Queries
    *   Range Query Examples     
    *   Source document: http://aws.amazon.com/articles/1231
    *************************************************************************************/

    [TestFixture]
    public class RangeQueryTests
    {
        [SetUp]
        public void TestInitialize()
        {
            SampleData.CreateSampleData();
        }

        [TearDown]
        public void TestCleanup()
        {
            SampleData.DeleteSampleData();
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where Year > '1975' and Year < '2008'	
        * Description:          Retrieves all items that have a "Year" value between "1975" and "2008."	
        * Expected result:      1579124585, B000T9886K, B00005JPLW, B000SF3NGK
        ************************************************************************************/
        [Test]
        public void GreaterThanAndLessThanTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.Year.GreaterThan("1975") & a.Year.LessThan("2008") select a;

            Assert.AreEqual(4, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }


        /************************************************************************************
        * Example SQL:          select * from mydomain where Year between '1975' and '2008'
        * Description:          Retrieves all items that have a "Year" value between "1975" and "2008."
        * Expected result:      1579124585, B000T9886K, B00005JPLW, B000SF3NGK
        ************************************************************************************/
        [Test]
        public void BetweenTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.Year.Between("1975", "2008") select a;

            Assert.AreEqual(4, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where Rating = '***' or Rating = '*****'
        * Description:          Retrieves all items that have 3 (***) or 5 (*****) star rating 
        * Description:          This is a discontiguous range query that consists of two distinct values selected from the range of all possible values for the attribute. 
        * Expected result:      0385333498, B00005JPLW, B000SF3NGK
        ************************************************************************************/
        [Test]
        public void OrTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where a.Rating.Any("***") | a.Rating.Any("*****") select a;

            Assert.AreEqual(3, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }

        /************************************************************************************
        * Example SQL:          select * from mydomain where (Year > '1950' and Year < '1960') or Year like '193%' or Year = '2007'
        * Description:          Retrieves all items where the "Year" attribute is either between "1950" and "1960," falls in the nineteen-thirties, or equals "2007." 
        * Expected result:      0385333498, 0802131786, B000T9886K, B00005JPLW
        ************************************************************************************/
        [Test]
        public void MultiCriteriaTest()
        {
            var dbContext = new AwsSampleContext();
            var all = from a in dbContext.Books where (a.Year.GreaterThan("1950") & a.Year.LessThan("1960") | a.Year.StartsWith("193") | a.Year == "2007") select a;

            Assert.AreEqual(4, all.ToList().Count());
            //Assert.AreEqual("1579124585", all.ToList().First().Id);
        }
    }
}
